package com.yuju.hotel.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.hotel.dao.IPriceSchemeDao;
import com.yuju.hotel.dao.entity.PriceScheme;
import com.yuju.hotel.enums.CustomersEnum;
import com.yuju.hotel.model.PriceDetailModel;
import com.yuju.hotel.model.PriceSchemeModel;
import com.yuju.hotel.model.RoomTypeModel;
import com.yuju.hotel.service.IPriceDetailService;
import com.yuju.hotel.service.IPriceSchemeService;
import com.yuju.hotel.service.IRoomTypeService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：PriceSchemeServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月28日 下午2:47:29  <br />
 * @version : 1.1 <br />
 */
@Service
public class PriceSchemeServiceImpl extends AbstractBaseService<PriceSchemeModel, PriceScheme, String> implements IPriceSchemeService{

	@Autowired
	private IPriceSchemeDao priceSchemeDao;
	@Autowired
	private IPriceDetailService priceDetailService;
	@Autowired
	private IRoomTypeService roomTypeService;
	
	@Override
	public IBaseDao<PriceSchemeModel, PriceScheme, String> getDao() {
		// TODO Auto-generated method stub
		return priceSchemeDao;
	}

	/**
	 * 
	 * 功能: 获取价格方案列表<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月29日 上午9:04:19<br/>
	 * 
	 * @param priceSchemeModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IPriceSchemeService#getPriceSchemeList(com.yuju.hotel.model.PriceSchemeModel)
	 */
	@Override
	public List<Map<String, Object>> getPriceSchemeList(PriceSchemeModel priceSchemeModel){
		// 查询参数
		Map<Object, Object> map = getBeanMap(priceSchemeModel);
		// 查询列表
		//List<PriceScheme> list = getDao().list(map);
		// 转换为PriceSchemeModel泛型List
		List<PriceSchemeModel> priceSchemeModelList = this.getDao().list(map);// convert(list);
		
		//拼接页面显示数据
		//创建一个装载返回对象的List
        List<Map<String, Object>> returnResult = new ArrayList<>();
        
        //根据分店ID查询本分店下的所有房型
        RoomTypeModel roomTypeModel = new RoomTypeModel();
        roomTypeModel.setHotelId(priceSchemeModel.getHotelId());
        List<RoomTypeModel> roomTypeList = roomTypeService.list(roomTypeModel);
        //拼接标题头
        Map<String, Object> title = new HashMap<>();
		
        title.put("col0", "方案ID");
        title.put("col1", "价格类别");
        title.put("col2", "价格方案");
        for(Integer i = 0; i < roomTypeList.size(); i++){
            Integer col = i + 3;
            String name = "col" + col.toString();
            title.put(name, roomTypeList.get(i).getName());
        }
        
        //添加到返回数据
        returnResult.add(title);
        //拼接数据
        Byte currPriceType = 0;
      	for(int i = 0; i < priceSchemeModelList.size(); i++){
      		//装载行数据
      		Map<String, Object> row = new HashMap<>();
      		//第一列:方案ID
      		row.put("col0", priceSchemeModelList.get(i).getId());
      		//第二列：列表查询以schemeType排序，匹配对应的方案类型
      		if (currPriceType != priceSchemeModelList.get(i).getSchemeType()) {
                currPriceType = priceSchemeModelList.get(i).getSchemeType();
                if(currPriceType != null){
                	row.put("col1", CustomersEnum.getTypeName(currPriceType.byteValue()));
                }
            } else {
                row.put("col1", "");
            }
      		//第三列：方案名称
      		row.put("col2",  priceSchemeModelList.get(i).getName());
      		
      		//根据方案ID查询对应的价格明细
      		PriceDetailModel priceDetailModel = new PriceDetailModel();
    		priceDetailModel.setPirceSchemeId(priceSchemeModelList.get(i).getId());
      		List<PriceDetailModel> priceDetailList = priceDetailService.list(priceDetailModel);
      		//按序排列到每列
      		for (int j = 0; j <priceDetailList.size(); j++) {
                Integer col = j + 3;
                String name = "col" + col.toString();
                row.put(name, priceDetailList.get(j).getPrice());
            }
      		//添加到返回数据
      		returnResult.add(row);
      	} 
		return returnResult;
	}
	
	

	/**
	 * 
	 * 功能:新增价格方案<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月28日 下午2:47:34<br/>
	 * 
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#getDao()
	 */

	@Override
	public int savePriceScheme(PriceSchemeModel priceSchemeModel, List<PriceDetailModel> priceDetailModels){
		//价格方案保存
		priceSchemeModel.setId(Utils.getUUID());
		priceSchemeModel.setGmtCreate(new Date());
		priceSchemeModel.setGmtModified(new Date());
		this.getDao().saveSelective(priceSchemeModel);
		//价格明细保存
		for(int i= 0; i <priceDetailModels.size(); i++){
			priceDetailModels.get(i).setPirceSchemeId(priceSchemeModel.getId());
			priceDetailService.save(priceDetailModels.get(i));
		}
		return 1;
	} 
	
	/**
	 * 
	 * 功能: 更新价格方案<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月28日 下午2:47:53<br/>
	 * 
	 * @param priceSchemeModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int updatePriceScheme(PriceSchemeModel priceSchemeModel, List<PriceDetailModel> priceDetailModels){
		//价格方案更新
		priceSchemeModel.setGmtModified(new Date());
		this.getDao().updateSelective(priceSchemeModel);
		//价格明细更新
		for(int i= 0; i < priceDetailModels.size(); i++){
			priceDetailModels.get(i).setHotelId(priceSchemeModel.getHotelId());
			priceDetailModels.get(i).setGroupId(priceSchemeModel.getGroupId());
			if(Utils.isNotNull(priceDetailModels.get(i).getId())){
				priceDetailService.updateSelective(priceDetailModels.get(i));
			}else {
				priceDetailModels.get(i).setPirceSchemeId(priceSchemeModel.getId());
				priceDetailService.save(priceDetailModels.get(i));
			}
		}	
		return 1;
	}
	/**
	 * 
	 * 功能: 根据ID查询信息<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月29日 下午4:44:37<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#findById(java.lang.Object)
	 */
	@Override
	public PriceSchemeModel findById(String id){
		PriceScheme priceScheme = getDao().getObj(id);
		if (priceScheme != null) {
			//根据ID查询价格明细
			PriceDetailModel priceDetailModel = new PriceDetailModel();
			priceDetailModel.setPirceSchemeId(id);
			List<PriceDetailModel> priceDetailList = priceDetailService.list(priceDetailModel);
			//转换类型
			PriceSchemeModel priceSchemeModel = convert(priceScheme);
			priceSchemeModel.setPriceDetailModels(priceDetailList);
			return priceSchemeModel;
		}
		return null;
	}

	/**
	 * 
	 * 功能:/新增分店时为分店添加价格方案原始数据 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月7日 下午3:09:29<br/>
	 * 
	 * @param hotelId
	 * @param groupId
	 * @return <br/>
	 * @see com.yuju.hotel.service.IPriceSchemeService#saveHotelAfter(java.lang.String, java.lang.String)
	 */
	@Override
	public int saveHotelAfter(String hotelId, String groupId) {
		PriceSchemeModel priceSchemeModel = new PriceSchemeModel();
		priceSchemeModel.setId(Utils.getUUID());
		priceSchemeModel.setHotelId(hotelId);
		priceSchemeModel.setGroupId(groupId);
		priceSchemeModel.setName("门市价");
		priceSchemeModel.setSchemeType((byte)1);
		priceSchemeModel.setGmtCreate(new Date());
		priceSchemeModel.setGmtModified(new Date());
		this.getDao().saveSelective(priceSchemeModel);
		priceSchemeModel.setId(Utils.getUUID());
		priceSchemeModel.setHotelId(hotelId);
		priceSchemeModel.setGroupId(groupId);
		priceSchemeModel.setName("微信会员");
		priceSchemeModel.setSchemeType((byte)2);
		priceSchemeModel.setGmtCreate(new Date());
		priceSchemeModel.setGmtModified(new Date());
		this.getDao().saveSelective(priceSchemeModel);
		
		return 1;
	}

	/**
	 * 
	 * 功能: 重名验证<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:20:39<br/>
	 * 
	 * @param priceSchemeModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IPriceSchemeService#selectByName(com.yuju.hotel.model.PriceSchemeModel)
	 */
	@Override
	public boolean selectByName(PriceSchemeModel priceSchemeModel) {
		// TODO Auto-generated method stub
		//定义返回值
		boolean result = false;
		//装载数据查询数据库
		PriceSchemeModel model = new PriceSchemeModel();
		model.setName(priceSchemeModel.getName());
		model.setHotelId(priceSchemeModel.getHotelId());
		
		//修改时验证重名
		if (Utils.isNotNull(priceSchemeModel.getId())) {
			PriceSchemeModel priceSchemeModel1 = convert(this.getDao().getObj(priceSchemeModel.getId()));
			if (priceSchemeModel.getName().equals(priceSchemeModel1.getName())) {
			        result = true;
			    } else {
			   	 	Map<Object, Object> beanMap = getBeanMap(model);
			        List<PriceSchemeModel> list = this.getDao().list(beanMap);
			        if (list.size() == 0) {
			            result = true;
			        }
			    }
		//新增验证重名
		}else {
			Map<Object, Object> beanMap = getBeanMap(model);
			    List<PriceSchemeModel> list = this.getDao().list(beanMap);
			    if (list.size() == 0) {
			        result = true;
			    }
			}
		return result;
	}

	/**
	 * 
	 * 功能:换房页面  根据换房类型 和 客源类型 价格方案联动 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月20日 下午1:48:48<br/>
	 * 
	 * @param priceSchemeModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IPriceSchemeService#priceSchemeDropdownList(com.yuju.hotel.model.PriceSchemeModel)
	 */
	@Override
	public List<PriceSchemeModel> priceSchemeDropdownList(PriceSchemeModel priceSchemeModel) {
		//根据客源类型和hotelId查询出相对应得价格方案
		PriceSchemeModel pModel = new PriceSchemeModel();
		pModel.setHotelId(priceSchemeModel.getHotelId());
		pModel.setSchemeType(priceSchemeModel.getSchemeType());
		List<PriceSchemeModel> list = this.getDao().list(getBeanMap(pModel));
		if(list.size() != 0){
			//根据价格方案ID和房型ID查询出对应的价格明细
			for (int i = 0; i < list.size(); i++) {
				PriceDetailModel priceDetailModel = new PriceDetailModel();
				priceDetailModel.setPirceSchemeId(list.get(i).getId());
				priceDetailModel.setRoomTypeId(priceSchemeModel.getRoomTypeId());
				List<PriceDetailModel> priceDetailModels = priceDetailService.list(priceDetailModel);
				list.get(i).setPriceDetailModels(priceDetailModels);
			}
			return list;
		}
		return null;
	}
	
}
